
/**
 * used to test some individual components
 * */

#include <stdio.h>
#include <stdlib.h>
#include <mian_util.h>

#include "dict_compression.h"


void test_RLEDictCompression() {

	const uint32 numRun = 400;
	const uint32 maxRunValue = 33;
	const uint32 maxRunLength = 11;

	/* generate data */
	int* gold_runValue = (int*)malloc(sizeof(int)*numRun);
	for(int i = 0; i < numRun; i++) {
		gold_runValue[i] = rand()%maxRunValue;
	}
	uint32* gold_runLength = (uint32*)malloc(sizeof(uint32)*numRun);
	for(int i = 0; i < numRun; i++) {
		gold_runLength[i] = rand()%maxRunLength + 1;
	}
	uint32 numElement = 0;
	for(int i = 0; i < numRun; i++) {
		numElement += gold_runLength[i];
	}
	int* gold_data = (int*)malloc(sizeof(int)*numElement);
	uint32 idx = 0;
	for(uint32 runId = 0; runId < numRun; runId++) {
		for(uint32 runOffset = 0; runOffset < gold_runLength[runId]; runOffset++) {
			gold_data[idx] = gold_runValue[runId];
			idx++;	
		}
	}


	/* RLE-dict compression */
	int* ref_data = (int*)malloc(sizeof(int)*numElement);
	memcpy(ref_data, gold_data, sizeof(int)*numElement);
	RLEDictData<int>* compressedData = new RLEDictData<int>(numElement);
	compressedData->compress(ref_data, numElement);
	printf("before compression data size: %lu bytes\n", sizeof(int)*numElement);
	printf("after compression data size: %lu bytes\n", compressedData->getDataSize());

	/* RLE-dict decompression */
	memset(ref_data, 0, sizeof(int)*numElement);
	const uint64 bufSize = sizeof(int)*numElement;
	byte* buf = (byte*)malloc(bufSize);
	compressedData->decompress(ref_data, buf, bufSize);


	/* check results */
	for(uint64 i = 0; i < numElement; i++) {
		if(gold_data[i] != ref_data[i]) {
			printf("i = %lu: gold_data = %d, ref_data = %d\n", i, gold_data[i], ref_data[i]);
			ERROR_EXIT("TEST FAILED!!!");
		}
	}
	printf("Test passed :)\n");
       
	compressedData->close();
}

int main() {
	
	test_RLEDictCompression();	

	return EXIT_SUCCESS;
}

